//
//(C) Copyright 2011 HongKong Airline.,Ltd. 
//
/**
 * messageDaoHibernate
 *
 * 项目名称:代购系统
 *
 * @author Mengxiangliang
 *
 * 功能描述:
 * 
 * 历史版本:2013-01-04 v1.0.0 Mengxiangliang  创建:
 * 
 */
package com.gamutsoft.amb.message.dao.impl;

import java.util.List;
import java.util.Map;

import com.gamutsoft.amb.common.bean.QueryExample;
import com.gamutsoft.amb.common.dao.impl.GenericDaoHibernate;
import com.gamutsoft.amb.common.exception.BusinessException;
import com.gamutsoft.amb.message.dao.IMessageDao;
import com.gamutsoft.amb.message.model.Message;
import com.gamutsoft.amb.messageuser.model.MessageUser;

@SuppressWarnings("unchecked")
public class MessageDaoHibernate extends GenericDaoHibernate<Message, Long> implements IMessageDao {
    
     public MessageDaoHibernate() {
		super(Message.class);

	}
    
    /**
	 * 根据条件查询记录
	 * @author Mengxiangliang
	 * @param  QueryExample
	 * @return List<Map>
	 * @throws BusinessException
	 */
	@Override
	public List<Map> searchMessageList(QueryExample qe) throws BusinessException {
	
	    StringBuffer sb = new StringBuffer();
		sb.append("SELECT T.ID,T.TITLE,T.CONTENT,T.CREATE_TIME,T.STATUS FROM MESSAGE T");
		//qe.addOrderBy("");
		return this.findByQueryExample(sb.toString(), qe);
		
	}

	/**
	 * 根据ID查询记录
	 * @author Mengxiangliang
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	@Override
	public Message searchMessageById(Long id) throws BusinessException {
	
	  Message domain = new Message();
	   String hql = "from Message where id=?";
	   List<Message> list = this.getHibernateTemplate().find(hql, id);
	   if(list != null && list.size() >0){
		  domain = list.get(0);
	   }
	   return domain;
		
	}

	/**
	 * 删除记录
	 * @author Mengxiangliang
	 * @param id
	 * @throws BusinessException
	 */
	@Override
	public void deleteMessageById(Long id) throws BusinessException {
	
	 String hql ="delete from Message where id=?";
	 this.getHibernateTemplate().bulkUpdate(hql, id);
	 
	}

	@Override
	public List<Map> searchMessageByWhere(QueryExample qe, MessageUser domain)
			throws BusinessException {
		 StringBuffer sb = new StringBuffer();
			sb.append("SELECT T.ID,T.TITLE,U.PERSON_IMG,U.USERNAME,T.CONTENT,T.CREATE_TIME,T.STATUS FROM MESSAGE T ");
			sb.append(" LEFT JOIN MESSAGE_USER MU ON MU.MESSAGE_ID=T.ID ");
			sb.append(" LEFT JOIN  SYSTEM_USER U ON U.ID=MU.USER_ID ");
			if(domain!=null){
				if(domain.getStatus()!=null && !"".equals(domain.getStatus())){
					qe.addCondictions("MU.STATUS","=",domain.getStatus());
				}
				if(domain.getUserId()!=null){
					qe.addCondictions("MU.USER_ID","=",domain.getUserId());
				}
			}
			qe.addOrderBy("T.CREATE_TIME DESC");
			return this.findByQueryExample(sb.toString(), qe);
	}
	
}